package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ywgt.pojo.Goods;
import com.ywgt.pojo.Groupon;
import com.ywgt.pojo.GrouponRules;
import com.ywgt.service.GoodsService;
import com.ywgt.service.GrouponRulesService;
import com.ywgt.service.GrouponService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.models.auth.In;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/groupon")
@ApiModel("团购管理")
public class GrouponController {
    @Autowired
    GrouponService grouponService;

    @Autowired
    GrouponRulesService grouponRulesService;

    @Autowired
    GoodsService goodsService;
    /**
     *  page: 1,
     *         limit: 20,
     *         goodsId: undefined,
     *         sort: 'add_time',
     *  order: 'desc'
     * @return
     */
    @GetMapping("/listRecord")
    public Object getGroupon(Integer grouponId, @RequestParam(required = false,defaultValue = "add_time") String sort,
                             @RequestParam(required = false,defaultValue = "desc") String order,
                             @RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "10") int limit){
        Page<Groupon> groupons=grouponService.getGroupon(grouponId,page,limit,sort,order);

        List<Map<String,Object>> records=new ArrayList<>();
        for (Groupon groupon : groupons) {
            Map<String, Object> recordData = new HashMap<>();
            Map<String,Object> search=new HashMap<>();
            search.put("groupon_id",groupon.getId());
            List<Groupon> subGrouponList = grouponService.listByMap(search);
            GrouponRules rules = grouponRulesService.getById(groupon.getRulesId());
            Goods goods =null;
            if(rules!=null){
                goods = goodsService.getById(rules.getGoodsId());
            }
            recordData.put("groupon", groupon);
            recordData.put("subGroupons", subGrouponList);
            recordData.put("rules", rules);
            recordData.put("goods", goods);
            records.add(recordData);
        }
        long total = groupons.getTotal();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",records);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/list")
    public Object getGrouponRole(Integer rulesId, @RequestParam(required = false,defaultValue = "add_time") String sort,
                                 @RequestParam(required = false,defaultValue = "desc") String order,
                                 @RequestParam(required = false,defaultValue = "1") int page,
                                 @RequestParam(required = false,defaultValue = "10") int limit){
        Page<GrouponRules> grouponRules=grouponRulesService.getGrouponRules(rulesId,page,limit,sort,order);
        List<GrouponRules> items = grouponRules.getResult();
        long total = grouponRules.getTotal();
        Map<String, Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",items);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody GrouponRules grouponRules){
        if(grouponRulesService.removeById(grouponRules.getId())){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/update")
    public Object update(@RequestBody GrouponRules grouponRules){
        if(grouponRulesService.updateById(grouponRules)){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/create")
    public Object create(@RequestBody GrouponRules grouponRules){
        Long goodsId = grouponRules.getGoodsId();
        Goods goods = goodsService.getById(goodsId);
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        if(grouponRulesService.save(grouponRules)){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }
}
